<?php

require_once APPLICATION_PATH . "/models/MovieMapper.php";
require_once "adapters/Adapter.php";

class MainController extends Zend_Controller_Action
{
    protected $_logger;

    /**
     * @var ISessionManager
     */
    protected $_sessionManager;

    /**
     * @var App_Model_Vo_Connection
     */
    protected $_connection;

    function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array()) {

        parent::__construct($request, $response, $invokeArgs);

        $this->_logger = Adapter::getLogger();

        //$this->_logger->debug();

        // FIXME
        $config_js = "/js/config.js";
        $bloub_js = "/js/bloub.js";
        $bloub_css = "/assets/bloub.css";
        if (isset($_SERVER['SERVER_NAME']) && $_SERVER['SERVER_NAME'] == 'localhost') {
            $config_js = '/bloub' . $config_js;
            $bloub_js = '/bloub' . $bloub_js;
            $bloub_css = '/bloub' . $bloub_css;
        }
        $this->view->headScript()->appendFile($config_js);
        $this->view->headScript()->appendFile($bloub_js);
        $this->view->headLink()->appendStylesheet($bloub_css);

        $this->_sessionManager = Adapter::getSessionManager();
        $connection = $this->_sessionManager->getConnection();

//         $this->_logger->debug("=== connection =======================");
//         $this->_logger->debug(print_r($connection, true));
//         $this->_logger->debug("==========================");

        if ($connection === false) {
//             $this->_logger->debug("=== connection false =======================");
            //             $url = $this->_sessionManager->getLoginPageUrl();
//             $url = "http://" . $_SERVER['SERVER_NAME'] . $url;

            $curr = Zend_Controller_Front::getInstance()->getRequest()->getControllerName();
//             $this->_logger->debug("curr<$curr> baseUrl<{$this->view->baseUrl()}>");
            if ($curr != 'index' && $curr != 'error')
                $this->_redirect("/");
        }

        $this->_connection = $connection;
        $this->view->connection = $connection;
    }
}
